package View;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Controller.DB;
import Model.Customer;
import Model.Order;

@SuppressWarnings("serial")
public class OrderDetailPopup extends JFrame {

	private JPanel contentPane;
	private RestUWantGUI myGUI;

	/**
	 * Create the frame.
	 */
	public OrderDetailPopup(final RestUWantGUI GUI, final Customer cust, final Order order) {
		
		myGUI = GUI;
		
		setTitle("Order Detail");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 600, 300);
		contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		CompletePanel compPanel = new CompletePanel(this, order);
		contentPane.add(compPanel, BorderLayout.CENTER);
		
		JButton btnOrder = new JButton("Re-Order");
		btnOrder.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int id;
				try {
					id = DB.getNextOrderID();
					order.setID(id);
					cust.setOrder(order);	
					GUI.database.addOrder(cust);
					GUI.orderToComplete();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				((JFrame) ((JButton) e.getSource()).getTopLevelAncestor()).dispose();
			}
			
		});
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
		    	((JFrame) ((JButton) e.getSource()).getTopLevelAncestor()).dispose();
			}
			
		});
		
		JPanel southPanel = new JPanel();
		southPanel.add(btnOrder);
		southPanel.add(btnClose);
		
		contentPane.add(southPanel, BorderLayout.SOUTH);
		
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}
